<?php

	/**
	 * 
	 */
	class SluzobneCesty_model extends CI_Model {
		
		function __construct() {
			parent::__construct();
		}
		
		function add($table,$data){
			return $this->db->insert($table,$data);
		}
		
		function zmen($table,$data){
			return $this->db->where('id',$data['id'])
					 	->update($table, $data);					 
		}
		
		function zmenPids($table, $id, $str){
			return $this->db->where('id', $id)
							->update($table, array('person_id'=>$str)); 
		}
		
		function delete($table,$id){
			return $this->db->delete($table, array('id'=>$id));
		}
		
		function Search($data){
			//TODO
		}
		
		function getOdCestaById($id){
			$q = $this->db->get_where('odcestovani',array('id'=>$id));
			if( $q->num_rows() > 0 ) return $q->result_array();
			else return FALSE;			
		}
		
		function getPriCestaById($id){
			$q = $this->db->get_where('pricestovani',array('id'=>$id));
			if( $q->num_rows() > 0 )return $q->result_array();
			else return FALSE;
		}
		
		function getOdCesty($filter = array('page' => 1, 'per_page' => 20)){
                        $this->db->from('odcestovani');
                        if (isset($filter['city']) && $filter['city'] !== '') {
                            $this->db->where('city', $filter['city']);
                        }
                        if (isset($filter['country']) && $filter['country'] !== '') {
                            $this->db->where('country', $filter['country']);
                        }
                        if (isset($filter['organization']) && $filter['organization'] !== '') {
                            $this->db->where('organization', $filter['organization']);
                        }
                        if (isset($filter['person_id'])&& is_integer($filter['person_id']) && (int)$filter['person_id'] > 0) {
                            $this->db->where('FIND_IN_SET(\'' . (int)$filter['person_id'] . '\', `person_id`)');
                        }
                        $q = $this->db->get();
                        echo $this->db->last_query();
			if( $q->num_rows() > 0 )return $q->result_array();
			else return FALSE;
		}
		
		function getPriCesty(){
			$q = $this->db->get('pricestovani');
			if( $q->num_rows() > 0 ) return $q->result_array();
			else return FALSE; 
		}
		
		function getCestuById($from, $id){
			$q = $this->db->get_where($from, array('id'=>$id));	
			if( $q->num_rows() > 0 ) return $q->result_array();
			else return FALSE;	
		}
		
		function kontrola($table, $str){
			$result = $this->db->get_where($table, array('unique'=>$str));
							   
			if( $result->num_rows() == 0 ){
				return FALSE;
			} else {
				return TRUE;
			}
		}
		
		function getByUnique($table, $str){
			$result = $this->db->where('unique',$str)
							   ->select('id')
							   ->get($table);
			if ( $result->num_rows() > 0 ){
				return $result->row_array();
			} else {
				return FALSE;
			}
		}
	}
	